package com.ssq.mobilehelper.aes;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RatingBar;
import android.widget.Spinner;
import android.widget.Toast;
import com.ssq.mobilehelper.global.AppSettings;
import com.ssq.mobilehelper.global.MyApp;
import com.ssq.mobilehelper.R;
import com.ssq.mobilehelper.greendao.gen.EncryptBeanDao;

import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 *
 * 添加加密界面
 */

public class AddEncryptActivity extends AppCompatActivity implements View.OnClickListener,
        RatingBar.OnRatingBarChangeListener,
        AdapterView.OnItemSelectedListener {

    private EditText mEditAccount;
    private EditText mEditPassword;
    private Spinner mSpinner;
    private RatingBar mRatingBar;
    private EditText mEditExtra;
    private Button btnSubmit;
    private  EncryptBean encryptBean = new EncryptBean();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_encrypt);
        initView();
        setTitle("添加");
    }

    /**
     * 初始化控件
     */
    private void initView() {
        mEditAccount = (EditText) findViewById(R.id.editAccount);
        mEditPassword = (EditText) findViewById(R.id.editPassword);
        mSpinner = (Spinner) findViewById(R.id.spinner);
        mRatingBar = (RatingBar) findViewById(R.id.ratingBar);
        mEditExtra = (EditText) findViewById(R.id.editExtra);
        btnSubmit = (Button) findViewById(R.id.btn_submit);
        btnSubmit.setOnClickListener(this);
        mSpinner.setOnItemSelectedListener(this);
        mRatingBar.setOnRatingBarChangeListener(this);
    }

    private void submit() throws UnsupportedEncodingException, NoSuchAlgorithmException {
        // validate
        String editAccountString = mEditAccount.getText().toString().trim();
        if (TextUtils.isEmpty(editAccountString)) {
            Toast.makeText(this, "填写帐户", Toast.LENGTH_SHORT).show();
            return;
        }

        String editPasswordString = mEditPassword.getText().toString().trim();
        if (TextUtils.isEmpty(editPasswordString)) {
            Toast.makeText(this, "填写密码", Toast.LENGTH_SHORT).show();
            return;
        }

        String editExtraString = mEditExtra.getText().toString().trim();
//        if (TextUtils.isEmpty(editExtraString)) {
//            Toast.makeText(this, "editExtraString不能为空", Toast.LENGTH_SHORT).show();
//            return;
//        }

        // TODO validate success, do something


        encryptBean.setAccount(editAccountString);
        String encrypt = null;

        try {
            encrypt = AESUtils.encrypt(AppSettings.PASSWORD,editPasswordString);
        } catch (Exception e) {
            e.printStackTrace();
        }
        encryptBean.setPassword(encrypt);


        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");

        encryptBean.setId(date.getTime());

        encryptBean.setCreate_time(""+sdf.format(date));
        encryptBean.setMessage(editExtraString);
        encryptBean.setLevel(mRatingBar.getRating());

        Log.d("TAG", "submit: "+encryptBean.toString());

        addDatabase();

    }




    @Override
    public void onClick(View view) {
        try {
            submit();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
    }

    private void addDatabase() {
        EncryptBeanDao encryptBeanDao = MyApp.getInstances().getDaoSession().getEncryptBeanDao();
        // 往表里插入一条新的数据
        encryptBeanDao.insert(encryptBean);
        Toast.makeText(this, "已保存", Toast.LENGTH_SHORT).show();
    }



//选择等级
    @Override
    public void onRatingChanged(RatingBar ratingBar, float v, boolean b) {
        ratingBar.setRating(v);

    }

    @Override
    public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
        String[] arrays = getResources().getStringArray(R.array.languages);
        encryptBean.setType(arrays[i]);

    }

    @Override
    public void onNothingSelected(AdapterView<?> adapterView) {



    }
}
